package com.ns.dcjh.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.ns.dcjh.MyApplication;
import com.ns.dcjh.R;
import com.ns.dcjh.base.BaseActivity;
import com.ns.dcjh.constant.Constant;
import com.ns.dcjh.utils.BitmapUtils;
import com.ns.dcjh.utils.CrashHandler;
import com.ns.dcjh.utils.IntentUtils;
import com.ns.dcjh.utils.SharedPreferencesUtils;
import com.ns.dcjh.utils.upyun.UpYunCompleteListener;
import com.ns.dcjh.utils.upyun.UpYunEntity;
import com.ns.dcjh.utils.upyun.UpYunUtil;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Date;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class SplashActivity extends BaseActivity {
    @BindView(R.id.img_splash)
    ImageView imageViewSplash;

    private SharedPreferencesUtils sp;

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.at_rest, R.anim.fade_out);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @SuppressLint("CommitPrefEdits")
    @Override
    protected void initData() {
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = SharedPreferencesUtils.getInstance(mContext);
        if (!setAgreePolicy()) {
            setImage();
        }
    }

    @Override
    protected void initView() {
        if (!isTaskRoot()) {
            finish();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        finish();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void dataRequest() {

    }

    private boolean setAgreePolicy() {
        String userAgreePolicy = String.valueOf(sp.objectForKey(Constant.SP_KEY_USER_AGREE_PRIVACY_POLICY, ""));
        if (StringUtils.isEmpty(userAgreePolicy)) {
            IntentUtils.showIntent(mContext, UserPrivacyPolicyDialogActivity.class);
            return true;
        }
        return false;
    }

    private void setImage() {
        String splashImg = String.valueOf(sp.objectForKey(Constant.SP_KEY_SPLASH_IMG, ""));
        if (StringUtils.isNotEmpty(splashImg)) {
            Glide.with(mContext)
                    .load(BitmapUtils.base64ToBitmap(splashImg, this))
                    .dontAnimate()
                    .into(imageViewSplash);
        }
        ((MyApplication) getApplication()).initThirdSdk();
        new Handler().postDelayed(() -> {
            IntentUtils.showIntent(mContext, WebviewX5Activity.class);
            finish();
        }, 2 * 1000);
    }
}